//
//  YJModuleListViewController.swift
//  YJIOSFunctionModuleSwift
//
//  Created by o dream boy on 2018/4/9.
//  Copyright © 2018年 cool. All rights reserved.
//

import UIKit
import Then
import RxDataSources
import RxSwift
import RxCocoa
import NSObject_Rx
import SnapKit

class YJModuleListViewController: YJBaseViewController {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView().then {
            $0.tableFooterView = UIView()
            $0.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        }
        return tableView
    }()
    
    lazy var dataSource: RxTableViewSectionedReloadDataSource<SectionModel<String, String>> = {
        let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, String>>.init(configureCell: { (_, tableView, indexPath, value) -> UITableViewCell in
            let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
            cell?.textLabel?.text = value
            return cell!
        })
        return dataSource
    }()

    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    override func setupUI() {
        
        tableView.rx.setDelegate(self).disposed(by: rx.disposeBag)
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        // 存放着解析完成的模型数组
        let models = Variable<[String]>([])
        
        let sections : Driver<[SectionModel<String, String>]> =
            models.asObservable().map { (strings) -> [SectionModel<String, String>] in
                return [SectionModel(model: "", items: strings)]
        }.asDriver(onErrorJustReturn: [])
        
        sections.asDriver().drive(tableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)
        
        tableView.rx.itemSelected.map { (indexPath)in
            return (indexPath, self.dataSource[indexPath])
            }.subscribe(onNext: { (indexPath, model) in
                print(indexPath,model)
                
                let VC = YJEncryptionViewController()
                VC.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(VC, animated: true)
                
            }).disposed(by: rx.disposeBag)
        
        models.value = ["加解密"]
    }
}

extension YJModuleListViewController: UITableViewDelegate {}
